package com.xtm.exercise.base.A08map.map03;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @author lei
 * @version 1.0
 * @date 2023/3/14 8:48
 */
public class DemoIfMap {

    /**
     * 使用map做if..else判断
     */
    @Test
    public void test01(){
        String str = "01";
        int i = transformInvoiceForm2Front(str);
        System.out.println(i);

    }

    private int transformInvoiceForm2Front(String settleInvoiceForm) {
        final Map<String, Integer> transformMap = Maps.newHashMap();
        transformMap.put("01", 1);
        transformMap.put("02", 2);
        transformMap.put("03", 3);
        Integer integer = transformMap.get(settleInvoiceForm);
        return integer;
    }

    // 两个list集合，通过map获取相同属性的数据，修改另外一个集合的数据
    @Test
    public void test02(){
        List<StudentMap> studentList1 = Lists.newArrayList();
        StudentMap student =  new StudentMap("laodb3", 32, "tj");
        StudentMap student1 = new StudentMap("laodb1", 32, "bj");
        StudentMap student2 = new StudentMap("laodb2", 32, "sh");
        StudentMap student3 = new StudentMap("laodb1", 32, "zj");
        studentList1.add(student);
        studentList1.add(student1);
        studentList1.add(student2);
        studentList1.add(student3);

        List<StudentMap> excel = Lists.newArrayList();
        StudentMap stu =  new StudentMap("laodb3", 32,"sq");
        StudentMap stu1 = new StudentMap("laodb2", 32,"sq");
        excel.add(stu);
        excel.add(stu1);

        ImmutableMap<String, StudentMap> getPurchaseLimit = Maps.uniqueIndex(excel, e -> e.getName() + e.getAge());

        for (StudentMap stude : studentList1) {
            StudentMap studentNew = getPurchaseLimit.get(stude.getName() + stude.getAge());
            if (studentNew != null) {
                stude.setAddress(studentNew.getAddress());
            }
        }
        System.out.println(JSON.toJSONString(studentList1));// 集合的地址属性的值，变更了
    }

}
